<?php
namespace app\admin\controller;

use app\AdminCollection;
use My\Validate;
use My\Tool;
use app\model\Members;
use app\model\UpdateBalancemodel;
use app\model\IntegralLogmodel;
use app\model\LoginLogmodel;
use app\model\Admin;
use think\Db;

class Member extends AdminCollection
{

    public function __construct()
    {
        parent::__construct();
    }

    public function index()
    {
        $input = input('');
        $page = isset($input['page']) ? $input['page'] : 1;
        $info = isset($input['info']) ? $input['info'] : ['filed_name' => '', 'filed_content' => '', 'status' => ''];
        $filed_name = $info['filed_name'];
        $filed_content = $info['filed_content'];
        $status = $info['status'];
        $where = array();
        if ($filed_name && $filed_content) {
            $where[$filed_name] = array('like', '%' . $filed_content . '%');
        }
        if ($status) {
            switch ($status) {
                case 10:
                    $statusvalue = 0;
                    break;
                default:
                    $statusvalue = $status;
                    break;
            }
            $where['status'] = $statusvalue;
        }
        $list = Members::pageService($where);
        $orderkey = pagesize * ($page - 1);
        $_list = array();
        foreach ($list as $key => $val) {
            $_t = $val;
            $_t['orderkey'] = $orderkey += 1;
            $_t['balance'] = Tool::formatbalance($val['balance']);
            $_t['login_time'] = $val['login_time'] != 0 ? date('Y-m-d H:i:s', $val['login_time']) : '0000-00-00 00:00:00';
            $_t['statusstring'] = $this->getConfig('admin_status', $val['status']);
            $_t['checkname'] = $this->getConfig('ischeck', $val['is_check']);
            $_t['deleteurl'] = url('member/delete', array('member_id' => $val['member_id']));
            $_t['updateurl'] = url('member/update', array('member_id' => $val['member_id']));
            $_t['statusurl'] = url('member/status', array('member_id' => $val['member_id']));
            $_t['updatepriceurl'] = url('member/updateprice', array('member_id' => $val['member_id']));
            $_t['integrallogurl'] = url('member/integrallog', array('member_id' => $val['member_id']));
            $_t['loginlogurl'] = url('member/loginlog', array('member_id' => $val['member_id']));
            $_list[] = $_t;
        }
        $pages = $list->render();
        $this->assign('inputform', $info);
        $this->assign('list', $_list);
        $this->assign('pages', $pages);
        $this->assign('checkurl', url('member/allcheck'));
        return $this->fetch();
    }


    /**
     * 批量审核
     */
    public function allcheck()
    {
        Db::startTrans();
        try {
            $input = input('');
            if (Validate::checkEmpty($input)) throw new \Exception("请选择");
            $id = Tool::arrayString($input['checkbox']);
            $where = [];
            $where['member_id'] = ['in', $id];
            $return = Members::updateServer($where, ['is_check' => 1, 'check_time' => time()]);
            if ($return) {
                Db::commit();
                $this->returnSuccess("提交数据成功");
            } else {
                throw new \Exception("操作失败");
            }
        } catch (\Exception $e) {
            Db::rollback();
            $this->returnError($e->getMessage());
        }
    }

    //添加用户
    public function add()
    {
        $submit = input('submit');
        Db::startTrans();
        if ($submit) {
            try {
                $input = input('');
                $data = $input['info'];
                $username = $data['username'];
                $password = $data['password'];
                $confirmpassword = $data['confirmpassword'];
                $name = $data['name'];
                $integral = $data['integral'];
                if (Validate::checkNull($username)) throw new \Exception("请输入用户名");
                if (Validate::checkMinLength($username, 4)) throw new \Exception("请输入最少4位的用户名");
                if (Validate::checkNull($password)) throw new \Exception("请输入登录密码");
                if (Validate::checkMinLength($password)) throw new \Exception("请输入最少6位的密码");
                if (Validate::checkNull($confirmpassword)) throw new \Exception("请输入确认密码");
                if (Validate::checkNoEqual($password, $confirmpassword)) throw new \Exception("两次输入密码不一致");
                if (Validate::checkNull($name)) throw new \Exception("请输入您的姓名");
                $findService = Members::findService(['username' => $username]);
                if ($findService) throw new \Exception("用户已经存在，请重新输入用户名");
                $salt = Tool::randomNumber();
                $data['password'] = Tool::encryptionFunction($password, $salt);
                $data['salt'] = $salt;
                $data['register_ip'] = getIP();
                $data['register_time'] = time();
                unset($data['confirmpassword']);
                $return = Members::addService($data);
                if ($integral > 0) {
                    $dataintegral = [];
                    $dataintegral['admin_id'] = $this->getAdmin();
                    $dataintegral['member_id'] = $return;
                    $dataintegral['integral'] = $integral;
                    $dataintegral['plusless'] = 1;
                    $dataintegral['inputtime'] = time();
                    $dataintegral['describe'] = "管理员添加会员，赠送积分";
                    $integralreturn = IntegralLogmodel::addService($dataintegral);
                } else {
                    $integralreturn = 1;
                }
                if ($return && $integralreturn) {
                    Db::commit();
                    $this->returnSuccess("提交数据成功");
                } else {
                    throw new \Exception("提交数据失败");
                }
            } catch (\Exception $e) {
                Db::rollback();
                $this->returnError($e->getMessage());
            }
        } else {
            return $this->fetch();
        }
    }

    //编辑用户
    public function update()
    {
        $submit = input('submit');
        if ($submit) {
            try {
                $input = input('');
                $where = $input['where'];
                $data = $input['info'];
                $password = $data['password'];
                $confirmpassword = $data['confirmpassword'];
                $name = $data['name'];
                if ($password) {
                    if (Validate::checkNull($password)) throw new \Exception("请输入登录密码");
                    if (Validate::checkMinLength($password)) throw new \Exception("请输入最少6位的密码");
                    if (Validate::checkNull($confirmpassword)) throw new \Exception("请输入确认密码");
                    if (Validate::checkNoEqual($password, $confirmpassword)) throw new \Exception("两次输入密码不一致");
                    $salt = Tool::randomNumber();
                    $data['password'] = Tool::encryptionFunction($password, $salt);
                    $data['salt'] = $salt;
                    unset($data['confirmpassword']);
                } else {
                    unset($data['password']);
                    unset($data['confirmpassword']);
                }
                if (Validate::checkNull($name)) throw new \Exception("请输入您的姓名");
                Members::updateServer($where, $data);
                $this->returnSuccess("提交数据成功", $this->getPrevious());
            } catch (\Exception $e) {
                $this->returnError($e->getMessage());
            }
        } else {
            $member_id = input('member_id');
            if (Validate::checkNull($member_id)) $this->returnError('系统发生致命错误');
            $findService = Members::findService(['member_id' => $member_id]);
            if (Validate::checkEmpty($findService)) $this->returnError('系统发生致命错误');
            $findService['login_time'] = $findService['login_time'] != 0 ? date('Y-m-d H:i:s', $findService['login_time']) : '0000-00-00 00:00:00';
            $findService['register_time'] = $findService['register_time'] != 0 ? date('Y-m-d H:i:s', $findService['register_time']) : '0000-00-00 00:00:00';
            $findService['check_time'] = $findService['check_time'] != 0 ? date('Y-m-d H:i:s', $findService['check_time']) : '0000-00-00 00:00:00';
            $findService['checkname'] = $this->getConfig('ischeck', $findService['is_check']);
            $this->assign('findService', $findService);
            $this->assign('REFERER', $this->previousUrl());
            return $this->fetch();
        }
    }

    //删除用户
    public function delete()
    {
        try {
            $member_id = input('member_id');
            if (Validate::checkNull($member_id)) throw new \Exception('删除失败');
            $return = Members::deleteService(['member_id' => $member_id]);
            if ($return) {
                $this->returnSuccess("删除成功");
            } else {
                throw new \Exception("删除失败");
            }
        } catch (\Exception $e) {
            $this->returnError($e->getMessage());
        }
    }

    //更新余额
    public function updateprice()
    {
        $submit = input('submit');
        if ($submit) {
            Db::startTrans();
            try {
                $input = input('');
                $where = $input['where'];
                $data = $input['info'];
                $updatedata = array();
                $updatedata['admin_id'] = $this->getAdmin();
                $updatedata['member_id'] = $where['member_id'];
                $updatedata['balance'] = $data['balance'] * 100;
                $updatedata['plusless'] = $data['plusless'];
                $updatedata['inputtime'] = time();
                $findService = Members::findService(['member_id' => $where['member_id']]);
                if (Validate::checkEmpty($findService)) $this->returnError('系统发生致命错误');
                if (Validate::checkNull($data['balance'])) throw new \Exception("请输入修改余额");
                if (Validate::checkBalance($data['balance'])) throw new \Exception("请重新输入修改余额");
                if ($data['plusless'] == 1) {
                    $totalbalance = $findService['balance'] + $data['balance'] * 100;
                } else {
                    $totalbalance = $findService['balance'] - $data['balance'] * 100;
                }
                $balancereturn = UpdateBalancemodel::addService($updatedata);
                $memberreturn = Members::updateServer($where, ['balance' => $totalbalance]);
                if ($balancereturn && $memberreturn) {
                    Db::commit();
                    $this->returnSuccess("更新数据成功", $this->getPrevious());
                } else {
                    throw new \Exception("更新数据失败");
                }
            } catch (\Exception $e) {
                Db::rollback();
                $this->returnError($e->getMessage());
            }
        } else {
            $member_id = input('member_id');
            $page = isset($input['page']) ? $input['page'] : 1;
            if (Validate::checkNull($member_id)) $this->returnError('系统发生致命错误');
            $findService = Members::findService(['member_id' => $member_id]);
            if (Validate::checkEmpty($findService)) $this->returnError('系统发生致命错误');
            $findService['balance'] = Tool::formatbalance($findService['balance']);
            $this->assign('findService', $findService);
            $this->assign('REFERER', $this->previousUrl());

            $where = array();
            $where['member_id'] = $member_id;
            $orderkey = pagesize * ($page - 1);
            $list = UpdateBalancemodel::pageService($where);
            $_list = array();
            foreach ($list as $key => $val) {
                $_t = $val;
                $_t['balance'] = Tool::formatbalance($val['balance']);
                $_t['orderkey'] = $orderkey += 1;
                $_t['plusless'] = $this->getConfig('plusless', $val['plusless']);
                $adminfind = Admin::findService(['admin_id' => $val['admin_id']], ['name']);
                $_t['adminname'] = $adminfind['name'];
                $_t['inputtime'] = $val['inputtime'] != 0 ? date('Y-m-d H:i:s', $val['inputtime']) : '0000-00-00 00:00:00';
                $_list[] = $_t;
            }
            $pages = $list->render();
            $this->assign('list', $_list);
            $this->assign('pages', $pages);
            return $this->fetch();
        }
    }

    public function integrallog()
    {
        $member_id = input('member_id');
        $page = isset($input['page']) ? $input['page'] : 1;
        if (Validate::checkNull($member_id)) $this->returnError('系统发生致命错误');
        $findService = Members::findService(['member_id' => $member_id]);
        if (Validate::checkEmpty($findService)) $this->returnError('系统发生致命错误');
        $this->assign('findService', $findService);
        $this->assign('REFERER', $this->previousUrl());

        $where = array();
        $where['member_id'] = $member_id;
        $orderkey = pagesize * ($page - 1);
        $list = IntegralLogmodel::pageService($where);
        $_list = array();
        foreach ($list as $key => $val) {
            $_t = $val;
            $_t['orderkey'] = $orderkey += 1;
            $_t['plusless'] = $this->getConfig('plusless', $val['plusless']);
            $_t['inputtime'] = $val['inputtime'] != 0 ? date('Y-m-d H:i:s', $val['inputtime']) : '0000-00-00 00:00:00';
            $_list[] = $_t;
        }
        $pages = $list->render();
        $this->assign('list', $_list);
        $this->assign('pages', $pages);
        return $this->fetch();
    }

    /**
     * 登录记录
     * @return mixed
     */
    public function loginlog()
    {
        $member_id = input('member_id');
        $page = isset($input['page']) ? $input['page'] : 1;
        if (Validate::checkNull($member_id)) $this->returnError('系统发生致命错误');
        $findService = Members::findService(['member_id' => $member_id]);
        if (Validate::checkEmpty($findService)) $this->returnError('系统发生致命错误');
        $this->assign('findService', $findService);
        $this->assign('REFERER', $this->previousUrl());

        $where = array();
        $where['member_id'] = $member_id;
        $orderkey = pagesize * ($page - 1);
        $list = LoginLogmodel::pageService($where);
        $_list = array();
        foreach ($list as $key => $val) {
            $_t = $val;
            $_t['orderkey'] = $orderkey += 1;
            $_t['login_time'] = $val['login_time'] != 0 ? date('Y-m-d H:i:s', $val['login_time']) : '0000-00-00 00:00:00';
            $_list[] = $_t;
        }
        $pages = $list->render();
        $this->assign('list', $_list);
        $this->assign('pages', $pages);
        return $this->fetch();
    }


}
